<?php

class PageController extends Zend_Controller_Action 
{
    function init()
    {
        Zend_Loader::loadClass('Page');
        Zend_Loader::loadClass('Article');
        Zend_Loader::loadClass('Translation');
        $this->initView();
        $this->view->baseUrl = $this->_request->getBaseUrl();
        Zend_Session::start();
    }
    
    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        $Article = new Article();
        $this->view->main_menu = $Article->getPublished();
        
    }
        
    function indexAction()
    {
        $alias = $this->_request->getParam('article');
        if(!$alias) $alias = 'index.html';
        
        if(!isset($_SESSION['lang'])) $_SESSION['lang'] = Translation::getDefaultLanguage();
        
        $page = new Page();
        $pageInfo = $page->getPage($alias);
        if(!$pageInfo) $pageInfo = $page->getPage('index.html');
        
        if($_SESSION['lang']!=DEFAUL_LANGUAGE)
        { 
            $TR = new Translation();
            $pageInfo->content = $TR->getTranslation($pageInfo->id,'article','content',$_SESSION['lang']);
            $pageInfo->short_content = $TR->getTranslation($pageInfo->id,'article','short_content',$_SESSION['lang']);
            $pageInfo->subject = $TR->getTranslation($pageInfo->id,'article','subject',$_SESSION['lang']);
        }
        
        $this->view->content = $pageInfo->content;
        $this->view->title = $pageInfo->subject;
        $this->view->pageInfo = $pageInfo;
        
    }
    
    function listAction()
    {
        $category_alias = $this->_request->getParam('alias');
        
        $article = new Article();
        $this->view->items = $article->getArticlesByCategory($category_alias);
        
        if($_SESSION['lang']!=DEFAUL_LANGUAGE)
        { 
            $TR = new Translation();
            foreach($this->view->items as &$item) {
                $item['content'] = $TR->getTranslation($item['id'],'article','content',$_SESSION['lang']);
                $item['short_content'] = $TR->getTranslation($item['id'],'article','short_content',$_SESSION['lang']);
                $item['subject'] = $TR->getTranslation($item['id'],'article','subject',$_SESSION['lang']);
            }
        }
        
        $this->view->pageInfo = new stdClass();
        $this->view->pageInfo->alias = $category_alias;
        
        $this->view->title = $this->view->items[0]['name'];
        
        
        $this->render();
    }

}